package com.dayu.basic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.dayu.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayu.basic.mapper.BasicSupplierMapper;
import com.dayu.basic.domain.BasicSupplier;
import com.dayu.basic.service.IBasicSupplierService;
import org.springframework.util.ObjectUtils;

/**
 * 供应商管理Service业务层处理
 * 
 * @author HK
 * @date 2021-04-15
 */
@Service
public class BasicSupplierServiceImpl implements IBasicSupplierService 
{
    @Autowired
    private BasicSupplierMapper basicSupplierMapper;

    /**
     * 查询供应商管理
     * 
     * @param supplierId 供应商管理ID
     * @return 供应商管理
     */
    @Override
    public BasicSupplier selectBasicSupplierById(Long supplierId)
    {
        return basicSupplierMapper.selectBasicSupplierById(supplierId);
    }

    /**
     * 查询供应商管理列表
     * 
     * @param basicSupplier 供应商管理
     * @return 供应商管理
     */
    @Override
    public List<BasicSupplier> selectBasicSupplierList(BasicSupplier basicSupplier)
    {
        return basicSupplierMapper.selectBasicSupplierList(basicSupplier);
    }

    /**
     * 新增供应商管理
     * 
     * @param basicSupplier 供应商管理
     * @return 结果
     */
    @Override
    public int insertBasicSupplier(BasicSupplier basicSupplier)
    {
        if(!ObjectUtils.isEmpty(basicSupplier.getMobile())) {
            BasicSupplier sel = new BasicSupplier();
            sel.setMobile(basicSupplier.getMobile());
            List<BasicSupplier> list = selectBasicSupplierList(sel);
            if(!list.isEmpty())
                throw new RuntimeException("当前手机号已存在");
        }
        basicSupplier.setCreateBy(SecurityUtils.getUsername());
        basicSupplier.setCreateTime(new Date());
        return basicSupplierMapper.insertBasicSupplier(basicSupplier);
    }

    /**
     * 修改供应商管理
     * 
     * @param basicSupplier 供应商管理
     * @return 结果
     */
    @Override
    public int updateBasicSupplier(BasicSupplier basicSupplier)
    {
        if(!ObjectUtils.isEmpty(basicSupplier.getMobile())) {
            BasicSupplier sel = new BasicSupplier();
            sel.setMobile(basicSupplier.getMobile());
            List<BasicSupplier> list = selectBasicSupplierList(sel);
            int num = 0;
            for (BasicSupplier item : list) {
                if(!Objects.equals(item.getSupplierId(), basicSupplier.getSupplierId()))
                    num++;
            }

            if(num > 0)
                throw new RuntimeException("当前手机号已存在");
        }
        basicSupplier.setUpdateBy(SecurityUtils.getUsername());
        basicSupplier.setUpdateTime(new Date());
        return basicSupplierMapper.updateBasicSupplier(basicSupplier);
    }

    /**
     * 批量删除供应商管理
     * 
     * @param supplierIds 需要删除的供应商管理ID
     * @return 结果
     */
    @Override
    public int deleteBasicSupplierByIds(Long[] supplierIds)
    {
        return basicSupplierMapper.deleteBasicSupplierByIds(supplierIds);
    }

    /**
     * 删除供应商管理信息
     * 
     * @param supplierId 供应商管理ID
     * @return 结果
     */
    @Override
    public int deleteBasicSupplierById(Long supplierId)
    {
        return basicSupplierMapper.deleteBasicSupplierById(supplierId);
    }
}
